package com.software.loginregister.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.software.loginregister.R;
import com.software.loginregister.adapter.CommunityAdapter;

public class CommunityFragment extends Fragment {
    private TabLayout tabCommunityLayout;
    private ViewPager2 vpCommunity;
    private CommunityAdapter communityAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_community, container, false);

        tabCommunityLayout = view.findViewById(R.id.tab_community_layout);
        vpCommunity = view.findViewById(R.id.vp_community);


        // 设置适配器
        communityAdapter = new CommunityAdapter(this);
        vpCommunity.setAdapter(communityAdapter);

        // 关联TabLayout和ViewPager2
        new TabLayoutMediator(tabCommunityLayout, vpCommunity, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("关注");
                    break;
                case 1:
                    tab.setText("推荐");
                    break;
                case 2:
                    tab.setText("热榜");
                    break;
            }
        }).attach();

        // 设置默认显示“推荐”页面
        vpCommunity.setCurrentItem(1, false);

        return view;
    }
}